package net.lab1024.smartadmin.module.business.easyComputeModel.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.constant.ResponseCodeConst;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.dto.EasyComputeModelAddDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.dto.EasyComputeModelQueryDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.dto.EasyComputeModelResultAddDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.dto.EasyComputeModelResultUpdateDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.vo.EasyComputeModelExcelVO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.vo.EasyComputeModelVO;
import net.lab1024.smartadmin.module.business.easyComputeModel.service.EasyComputeModelService;
import net.lab1024.smartadmin.module.business.easyComputeResult.domain.entity.EasyComputeResultEntity;
import net.lab1024.smartadmin.module.business.easyComputeResult.service.EasyComputeResultService;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * [ ??????????? ]
 *
 * @author ?Ζ?
 * @version 1.0
 * @company ????????
 * @copyright (c)  ????????Inc. All rights reserved.
 * @date 2021-11-27 13:58:04
 * @since JDK1.8
 */
@RestController
@Api(tags = {""})
public class EasyComputeModelController extends BaseController {

    @Autowired
    private EasyComputeModelService easyComputeModelService;
    @Autowired
    private EasyComputeResultService easyComputeResultService;

    @ApiOperation(value = "",notes = "@author ?Ζ?")
    @PostMapping("/easyComputeModel/page/query")
    public ResponseDTO<PageResultDTO<EasyComputeModelVO>> queryByPage(@RequestBody EasyComputeModelQueryDTO queryDTO) {
        return easyComputeModelService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "",notes = "@author ?Ζ?")
    @PostMapping("/easyComputeModel/add")
    public ResponseDTO<EasyComputeResultEntity> add(@RequestBody @Validated EasyComputeModelAddDTO addTO){
//       Long id=  easyComputeModelService.add(addTO);
        EasyComputeResultEntity easyComputeResultEntity  = new EasyComputeResultEntity(0L,
                0D, 0D, 0D,
                0D, 0D,0D);
        easyComputeResultEntity.setCreateTime(new Date());
        return ResponseDTO.succData(easyComputeResultEntity);
    }

    @ApiOperation(value = "",notes = "@author ?Ζ?")
    @PostMapping("/easyComputeModel/addAndResult")
    public ResponseDTO<String> addAndResult(@RequestBody @Validated EasyComputeModelResultAddDTO addTO){
        Integer num = easyComputeModelService.queryByName(addTO.getName());
        if(num>0){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_NAME_REPEAT);
        }
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        addTO.setUpdateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setUpdateTime(new Date());
       Long id=  easyComputeModelService.add(addTO);

        EasyComputeResultEntity addResultTO = SmartBeanUtil.copy(addTO, EasyComputeResultEntity.class);
        addResultTO.setModelId(id);
        easyComputeResultService.add(addResultTO);
        return ResponseDTO.succ();
    }

    @ApiOperation(value="",notes = "@author ?Ζ?")
    @PostMapping("/easyComputeModel/update")
    public ResponseDTO<String> update(@RequestBody @Validated EasyComputeModelResultUpdateDTO updateDTO){
        updateDTO.setUpdateUser(SmartRequestTokenUtil.getRequestUserId());
        updateDTO.setUpdateTime(new Date());
        return easyComputeModelService.update(updateDTO);
    }

    @ApiOperation(value="",notes = "@author ?Ζ?")
    @PostMapping("/easyComputeModel/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return easyComputeModelService.deleteByIds(idList);
    }

    @ApiOperation(value = "", notes = "@author ?Ζ?")
    @PostMapping("/easyComputeModel/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //
        List<EasyComputeModelExcelVO> easyComputeModelList = easyComputeModelService.queryBatchExportData(idList);
        //
        ExportParams ex = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(ex, EasyComputeModelExcelVO.class, easyComputeModelList);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String dateStr = simpleDateFormat.format(new Date());
        downloadExcel("简单计算模型"+dateStr, workbook, response);
    }

    @ApiOperation(value = "", notes = "@author ?Ζ?")
    @PostMapping("/easyComputeModel/export/all")
    public void exportAll(@RequestBody @Validated EasyComputeModelQueryDTO queryDTO, HttpServletResponse response) {
        //
        List<EasyComputeModelExcelVO> easyComputeModelList = easyComputeModelService.queryAllExportData(queryDTO);
        //
        ExportParams ex = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(ex, EasyComputeModelExcelVO.class, easyComputeModelList);
        downloadExcel("", workbook, response);
    }

}
